﻿//2014-01-06
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Service.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.BasicDataManage
{
    public class PrdTaskBusiness
    {
        private readonly PrdTaskService _dal = new PrdTaskService();

        private static List<PrdTaskEntity> GetList(DataTable dt)
        {
            try
            {
                var dr = dt.Select();
                return (from t in dr
                        select new PrdTaskEntity
                        {
                            TaskID = Guid.NewGuid(),
                            BizDate = StringToDateTime.EnDateToCnDate(t[0].ToString().Trim()),
                            ShiftName = t[1].ToString().Trim(),
                            PrdCode = t[2].ToString().Trim(),
                            EqCode = t[3].ToString().Trim(),
                            PlanQty = !string.IsNullOrEmpty(t[4].ToString()) ? Convert.ToInt32(t[4].ToString()) : 0,
                            PlanStartTime = StringToDateTime.EnDateTimeToCnDateTime(t[5].ToString().Trim()),
                            PlanEndTime = StringToDateTime.EnDateTimeToCnDateTime(t[6].ToString().Trim()),
                            ProcessIdx = !string.IsNullOrEmpty(t[7].ToString()) ? Convert.ToInt32(t[7].ToString()) : 0,
                            RecStatus = true
                        }).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void ImportPrdTask(string servicePath, string data)
        {
            var rows = (ArrayList)JsonHelper.Decode(data);
            var excelPath = "";
            foreach (Hashtable row in rows)
            {
                excelPath = row["excUrl"].ToString();
            }
            var paht = servicePath + excelPath;
            var dt = ExcelHelper.Import(paht);
            var list = GetList(dt);
            foreach (var t in list)
            {
                t.PrdID = new ProductService().GetPrdId(t.PrdCode);
                t.ShiftID = new ShiftService().GetShiftId(t.ShiftName);
                t.EqID = new EquipmentService().GetEqId(t.EqCode);
                t.FlowID = new FlowService().GetFlowID(t.PrdCode, t.ProcessIdx);
            }
            var gid = new Guid("00000000-0000-0000-0000-000000000000");
            var newlist = list.Where(i => !i.PrdID.Equals(gid) && !i.ShiftID.Equals(gid) && !i.EqID.Equals(gid) && !i.FlowID.Equals(gid)).ToList();
            var aList = _dal.GetAll();
            var sList = new List<PrdTaskEntity>();
            var uList = new List<PrdTaskEntity>();
            if (aList.Count > 0)
            {
                foreach (var l in newlist)
                {
                    var p = aList.Where(T => T.FlowID.Equals(l.FlowID) && T.BizDate.Equals(l.BizDate) && T.ShiftID.Equals(l.ShiftID) && T.EqID.Equals(l.EqID));
                    if (p.Count() > 0)
                    {
                        uList.Add(l);
                    }
                    else
                    {
                        sList.Add(l);
                    }
                }

                if (uList.Count > 0)
                {
                    _dal.BatchModify(uList);
                }
                if (sList.Count > 0)
                {
                    _dal.BatchAdd(sList);
                }
                uList.Clear();
                sList.Clear();
            }
            else
            {
                _dal.BatchAdd(list);
                list.Clear();
            }
        }

        public string GetPrdTaskList(PrdTaskQcEntity qcmodel)
        {
            var total = _dal.GetPrdTaskCount(qcmodel);
            var list = _dal.GetPrdTaskList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public void AddPrdTask(string data)
        {
            var model = new PrdTaskEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<PrdTaskEntity>();
            foreach (Hashtable row in rows)
            {
                model.BizDate = Convert.ToDateTime(row["BizDate"].ToString());
                model.ShiftID = new Guid(row["ShiftID"].ToString());
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {
                    var cmodel = new PrdTaskEntity();
                    cmodel.TaskID = Guid.NewGuid();
                    cmodel.BizDate = model.BizDate;
                    cmodel.ShiftID = model.ShiftID;
                    cmodel.FlowID = new Guid(item["FlowID"].ToString());
                    cmodel.EqID = new Guid(item["EqID"].ToString());
                    cmodel.PlanQty = item["PlanQty"] != null ? Convert.ToInt32(item["PlanQty"]) : 0;
                    cmodel.PlanStartTime = Convert.ToDateTime(item["PlanStartTime"]);
                    cmodel.PlanEndTime = Convert.ToDateTime(item["PlanEndTime"]);
                    cmodel.RecStatus = true;
                    list.Add(cmodel);
                }
            }
            if (list.Count > 0)
            {
                _dal.BatchAdd(list);
            }
        }

        public bool ModifyPrdTask(string data)
        {
            var model = new PrdTaskEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.TaskID = new Guid(row["TaskID"].ToString());
                model.FlowID = new Guid(row["FlowID"].ToString());
                model.BizDate = Convert.ToDateTime(row["BizDate"].ToString());
                model.ShiftID = new Guid(row["ShiftID"].ToString());
                model.EqID = new Guid(row["EqID"].ToString());
                model.PlanQty = Convert.ToInt32(row["PlanQty"].ToString());
                model.PlanStartTime = Convert.ToDateTime(row["PlanStartTime"].ToString());
                model.PlanEndTime = Convert.ToDateTime(row["PlanEndTime"].ToString());
            }
            return _dal.ModifyPrdTask(model);
        }

        public string GetPrdTaskInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetPrdTaskInfo(id));
        }
    }
}
